//
//  MainTabBar.swift
//  weibo
//
//  Created by 杨晨 on 16/3/8.
//  Copyright © 2016年 杨晨. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    //入口
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        addSubview(addBtn)
        
        
    }

    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
    }
    
    //addsubView时会被调用
    //当子视图的大小发生改变时也会被调用
    override func layoutSubviews() {
        //有override时一定要调用父类
        super.layoutSubviews()
        
        var index:CGFloat = 0
        let w = UIScreen.mainScreen().bounds.width/5;
        let h = self.bounds.height;
        let rect = CGRectMake(0, 0, w, h)
        
        for subview in subviews{
            
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                
                subview.frame = CGRectOffset(rect,index*w, 0)
                
                if index == 1 {
                   index++
                }
                
                index++
            }
            
        }
    
        //addSubview(button)
        addBtn.frame=CGRectOffset(rect, 2*w,0)
        //bringSubviewToFront(button)
    }
  
    lazy var addBtn: UIButton = {
        
        var tabBarbtn = UIButton()
        tabBarbtn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: .Normal)
        tabBarbtn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        tabBarbtn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        tabBarbtn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Normal)
        
        tabBarbtn.sizeToFit()
        return tabBarbtn;
        
    }()
    

    
}
